#!/bin/sh
###############################################################################
##  Author    : xuezhoyi
##  Name      : edw_function.sh
##  Functions : Define all common function
##  Purpose   : Define all common function
##  Revisions or Comments
##  VER        DATE        AUTHOR           DESCRIPTION
##---------  ----------  ---------------  ------------------------------------ 
##  1.0      2017-05-22  xuezhouyi        1. CREATED THIS SHELL.
###############################################################################

. ${0%`basename ${0}`}edw_parm.sh

# If date
function IF_DATE(){
    if [[ -n "${1}" && $(expr length ${1}) -eq 8 ]];then
        cal ${1:6:2} ${1:4:2} ${1:0:4} > /dev/null
        if [[ $? -eq 0 ]];then
            echo 0
        else
            echo 1
        fi
    else
        echo 1
    fi
}

# Day
function GET_CURR_DATE(){
    echo "$(date -d "${1}" +%Y%m%d)"
}
function GET_PREV_DATE(){
    echo "$(date -d "-1 days ${1}" +%Y%m%d)"
}
function GET_NEXT_DATE(){
    echo "$(date -d "1 days ${1}" +%Y%m%d)"
}
function GET_DAYS(){
    echo "$(date -d "${2} days ${1}" +%Y%m%d)"
}
function GET_LAST_YEAR_DATE(){
    echo "$(date -d "-12 months ${1}" +%Y%m%d)"
}
function GET_LAST_MONTH_DATE(){
     V_QUERY="SELECT DATE_FORMAT(DATE_ADD('${1}',INTERVAL -1 MONTH),'%Y%m%d');"
     V_RESULT=$(${V_RUN_MYSQL} -q "${V_QUERY}")
    echo "${V_RESULT}"
#    echo "$(date -d "-1 months ${1}" +%Y%m%d)"
}

# Month
function GET_CURR_MONTH(){
    echo "$(date -d "${1}" +%Y%m)"
}
function GET_PREV_MONTH(){
    V_QUERY="SELECT DATE_FORMAT(DATE_ADD('${1}',INTERVAL -1 MONTH),'%Y%m');"
    V_RESULT=$(${V_RUN_MYSQL} -q "${V_QUERY}")
    echo "${V_RESULT}"
 #   echo "$(date -d "-1 months ${1}" +%Y%m)"
}
function GET_NEXT_MONTH(){
    echo "$(date -d "1 months ${1}" +%Y%m)"
}
function GET_ADD_MONTHS(){
    V_QUERY="SELECT DATE_FORMAT(DATE_ADD('${1}',INTERVAL ${2} MONTH),'%Y%m%d');"
    V_RESULT=$(${V_RUN_MYSQL} -q "${V_QUERY}")
    echo "${V_RESULT}"
    #echo "$(date -d "${2} months ${1}" +%Y%m%d)"
}
function GET_MONTH_FIRST_DATE(){
    echo "$(date -d "${1}" +%Y%m)01"
}
function GET_LAST_YEAR_MONTH(){
    echo "$(date -d "-1 year ${1}" +%Y%m)"
}
function GET_LAST_N_MONTH(){
  V_QUERY="SELECT DATE_FORMAT(DATE_SUB('${1}',INTERVAL ${2} MONTH),'%Y%m');"
  V_RESULT=$(${V_RUN_MYSQL} -q "${V_QUERY}")
    echo "${V_RESULT}"
 #   echo "$(date -d "-${2} month ${1}" +%Y%m)"
}

# Quarter
function GET_CURR_QUARTER(){
    case ${1:4:2} in
        01|02|03) 
            echo "${1:0:4}1" 
        ;;
        04|05|06) 
            echo "${1:0:4}2" 
        ;;
        07|08|09) 
            echo "${1:0:4}3" 
        ;;
        10|11|12) 
            echo "${1:0:4}4" 
        ;;
    esac
}
function GET_PREV_QUARTER(){
    echo $(GET_CURR_QUARTER $(GET_MONTHS ${1} -3))
}
function GET_NEXT_QUARTER(){
    echo $(GET_CURR_QUARTER $(GET_MONTHS ${1} 3))
}

# Year
function GET_CURR_YEAR(){
    echo "$(date -d "${1}" +%Y)"
}
function GET_PREV_YEAR(){
    echo "$(date -d "-1 years ${1}" +%Y)"
}
function GET_NEXT_YEAR(){
    echo "$(date -d "1 years ${1}" +%Y)"
}
function GET_YEARS(){
    echo "$(date -d "${2} years ${1}" +%Y)"
}

# Extension function
# Extension function
# Extension function
function GET_MONTH_FINAL_DATE(){
    V_DAY=$(GET_ADD_MONTHS ${1} 1)
    V_DAY=$(GET_MONTH_FIRST_DATE ${V_DAY})
    V_DAY=$(GET_PREV_DATE ${V_DAY})
    echo "${V_DAY}"
}
function GET_BEGIN_MONTH(){
    V_PREV_YEAR=$(GET_PREV_YEAR ${1})
    V_BEGIN_MONTH="${V_PREV_YEAR}12"
    echo "${V_BEGIN_MONTH}"
}
function GET_LAST_MONTH(){
    V_PREV_YEAR=$(GET_PREV_YEAR ${1})
    V_CURR_MONTH=${1:4:2}
    V_LAST_MONTH="${V_PREV_YEAR}${V_CURR_MONTH}"
    echo "${V_LAST_MONTH}"
}
function GET_LAST_MONTH_END_DATE(){
    V_DAY=$(GET_MONTH_FIRST_DATE ${1})
    V_LAST_MONTH_END_DATE=$(GET_PREV_DATE ${V_DAY})
    echo "${V_LAST_MONTH_END_DATE}"
}
function GET_N_MONTH_END_DATE(){
    V_QUERY="SELECT DATE_FORMAT(DATE_SUB('${1}',INTERVAL ${2}-1 MONTH),'%Y%m');"
    V_RESULT=$(${V_RUN_MYSQL} -q "${V_QUERY}")
    echo "$(date -d "-1 days ${V_RESULT}01" +%Y%m%d)"   
}
function GET_PREV_YEAR_END_DATE(){
    V_PREV_YEAR=$(GET_PREV_YEAR ${1})
    V_PREV_YEAR_END_DATE="${V_PREV_YEAR}1231"
    echo "${V_PREV_YEAR_END_DATE}"
}
function GET_CURR_YEAR_START_DATE(){
    V_PREV_YEAR=$(GET_PREV_YEAR ${1})
    V_CURR_YEAR_START_DATE="${V_PREV_YEAR}1331"
    echo "${V_CURR_YEAR_START_DATE}"
}



